package Com.aop.dao;

import Com.aop.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoimpl extends  JdbcTemplate implements UserDao {
   /* @Autowired*/

    public int save(User user) {
        String sql = "INSERT INTO `myschool2`.`user`(`uname`,`upwd`)VALUES (?,?)";

        return update(sql,user.getUname(),user.getUpwd());
    }

    public int delete(User user) {
        String sql = "delete from `myschool2`.user1 where id = ?";
        return update(sql,user.getid());

    }

    public int update(User user) {
        String sql = "update `myschool2`.user1 name = ? ,upwd = ? where id = ?";
        return update(sql,user.getUname(),user.getUpwd(),user.getid());
    }

    public User queryUser(User user) {
        String sql = "SELECT `id`,`uname`,`upwd` FROM `myschool2`.`user` WHERE uname=? AND upwd=?";
        return queryForObject(sql,  new RowMapper<User>() {

            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user1=new User();
                user1.setid(resultSet.getInt("id"));
                user1.setUname(resultSet.getString("uname"));
                user1.setUpwd(resultSet.getString("upwd"));
                return user1;
            }
        },user.getUname(), user.getUpwd());
    }

    public List<User> querylist() {
        String sql="select *from user";

        return query(sql, new RowMapper<User>() {
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user1=new User();
                user1.setid(resultSet.getInt("id"));
                user1.setUname(resultSet.getString("uname"));
                user1.setUpwd(resultSet.getString("upwd"));
                return user1;
            }
        });
    }


}
